<template>
  <div class="cs-page-container">
    <div class="cs-filter-container">
      <el-select
        v-model="listQuery.orderType"
        clearable
        filterable
        placeholder="报警类型"
      >
        <el-option
          v-for="(channel, i) in typeitems"
          :key="i"
          :label="channel"
          :value="i"
        />
      </el-select>
      <el-date-picker
        v-model="daterangeValue"
        :picker-options="daterangePickerOptions"
        type="daterange"
        align="right"
        unlink-panels
        range-separator="至"
        start-placeholder="起始时间"
        end-placeholder="结束时间"
        value-format="yyyy-MM-dd"
        clearable
        @change="daterangeChangedTimestamp"
      />
      <el-button
        type="primary"
        icon="el-icon-search"
        :loading="listLoading"
        @click="handleFilter"
        >查询</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-refresh-right"
        @click="resetFilter"
        >重置</el-button
      >
      <el-button type="warning" @click="exportFile">导出</el-button>
    </div>
    <div class="cs-filter-result">
      <el-table
        v-loading="listLoading"
        :data="list"
        :height="'calc(100vh - 300px)'"
        border
        fit
        size="small"
        highlight-current-row
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
      >
        <el-table-column align="center" label="时间">
          <template slot-scope="{ row }">
            <span>{{ row.startTime | parseTime }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="报警类型">
          <template slot-scope="{ row }">
            <span>{{ typeitems[row.type] }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="报警原因">
          <template slot-scope="{ row }">
            <span>{{ row.message }}</span>
          </template>
        </el-table-column>
      </el-table>
      <TablePagination
        :current-page="listQuery.page"
        :limit="listQuery.limit"
        :total="total"
        :loading="listLoading"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChangeOffset"
      />
    </div>
  </div>
</template>

<script>
import TablePagination from '@/components/TablePagination'
import DATEPICKER_OPTIONS from '@/config/datepickerOptions'
import { xhrAbnormityWarning } from '@api/analysis'

export default {
  name: 'AbnormityWarning',
  components: { TablePagination },
  data() {
    return {
      list: [],
      total: 0,
      typeitems: {
        '0': '无订单',
        '1': '无订单（支付方式）',
        '2': '订单完成率异常',
        '3': '充值异常'
      },
      listQuery: {
        offset: 0,
        limit: 100,
        startTime: null,
        endTime: null,
        orderType: null,
        gender: null
      },
      daterangeValue: '',
      daterangePickerOptions: DATEPICKER_OPTIONS.daterangePickerOptions
    }
  },
  created() {
    this.doSearch()
  },
  methods: {
    doSearch() {
      xhrAbnormityWarning(this.listQuery).then(resp => {
        this.listLoading = false
        this.total = resp.data.count
        this.list = resp.data.list
      })
    }
  }
}
</script>
